﻿package com.map 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.system.ApplicationDomain;
	import flash.utils.setTimeout;
	
	import com.utils.DebugTrace;
	import com.utils.ExpenseTime;
	
	import com.controller.MapController;
	import com.event.EventZheng;
	import com.model.MapDataManage;
	
	import com.staticInfo.*;	
	
	import com.IFactory;
	import com.utils.TransformCoordinate;
	import com.element.*;
	import com.element.role.Man;
	import com.element.movieClip.Animal;
	import com.element.movieClip.ClipHouseParticle1;
	
	/**地图类,包括了tile类，还有obstacle。
	 * drawMap	画地图
	 * putObstacle	布置障碍
	 * seekRoad	寻路
	 * @author zhengzihua
	 */
	public class Map  extends Sprite
	{
		private var _mapController:MapController;
		private var _mapDataManage:MapDataManage;
		
		private var row:int;
		private var cols:int;
		private var contain:Sprite;//返回地图
		private var openList:Array;
		private var closeList:Object;
		
		//地图上拥有的元素
		private var targetP:Point;//目标点
		private var thingsArr:Array = [];//map上摆放着的物体
		private var _thingsNum:int;//摆放物体的数量
		private var _c:Array;//描述地图状态的数组,这个值应该放在MapDataManage中
		private var _siteNum:Point;//要摆放的物体占几格
		private var gridFrame:*;//描边的格子
		
		private var mapDeal:IMap;
		
		public function Map(controller:MapController,dataManage:MapDataManage) 
		{
			init();	
			this.mapDataManage = dataManage;
			this.mapController = controller;
			mapDataManage.addEventListener(EventConst.EVENT_MAPDATAMANAGE, onMapDataManageChange);
			addEventListener(EventConst.EVENT_TILE_MAP, onTile);
		}
		private function init():void{
			mapDeal= IFactory.getMap();
			contain = new Sprite();
			_c = new Array();
		}
		public function drawMap(row:int, cols:int):void {
			this.row = row;
			this.cols = cols;	
			mapDeal.drawMap(this, row, cols, _c);
		}
		/**
		 * Tile发送给Map的信息
		 * @param	e
		 */
		private function onTile(e:EventZheng):void 
		{
			var t:String = e.obj.type;
			switch(t) {
				case "click":
					var event:EventZheng = new EventZheng(EventConst.EVENT_MAP_MAIN);
					event.obj.type="tileClick"
					event.obj.point = e.obj.point;
					parent.dispatchEvent(event);
					break;
				case "rollOver":
					drawRectangle(e);
					break;
				case "rollOut":
					removeRectangle(e);
					break;
				default:
					break;
			}
		}
		/**
		 * mapDataManage改变
		 * @param	e
		 */
		private function onMapDataManageChange(e:EventZheng):void 
		{
			var changeType:String = e.obj.type;
			switch(changeType) {
				case "allowSite":
					setTileAllowPut(e.obj.allowSite);
					break;
				default:
					break;
			}
		}
		/**
		 * 
		 * @param	id	摆放物体的编号，根据编号来放置物体
		 * @param	p	摆放在map上的位置
		 */
		//public function putObstacle(id:int,p:Point):void {
			//var house:Clips = new ClipHouseParticle();
			//var cp:Point;
			//cp = TransformCoordinate.transformCoor(p);
			//house.x = cp.x;
			//house.y = cp.y;
			//house.sx = p.x;
			//house.sy = p.y;
			//_showObjectContain.addChild(house);
			//addThings(house);
		//}
		public function seekRoad(sp:Point, ep:Point):void {
			targetP = ep;
			mapDeal.seekRoad(sp, ep, _c,this);
		}
		/**
		 * 设置tile中的allowPut值，开始rollOver事件检测
		 * value 	设置tile的allowPut值
		 */
		public function setTileAllowPut(value:Boolean):void {
			for (var i:int = 0; i < row; i++ ) {
				for (var j:int = 0; j < cols; j++ ) {
					var tile:Tile = getChildByName("t" + i + "_" + j) as Tile;
					tile.allowPut = value;
				}
			}
		}
		/**
		 * 
		 * @param	tp	物体的行标和列表
		 * @param	row	物体占几行
		 * @param	cols	物体占几列
		 * @param	clip	放置什么物体
		 */
		public function putElement(tp:Point,row:int,cols:int,role:Man):void {
			if (checkPut(tp,row,cols)) {
				//摆放物体
				//1.根据row和cols计算出物体占的格子
				//2.将格子对应的值置为1
				//3.在起点坐标位置，放上物体		
				changeC(tp, row, cols, 1);	
				if (targetP == null || (targetP.x == role.sx && targetP.y == role.sy)) {
				}else {
					seekRoad(new Point(role.sx, role.sy), targetP);
				}
			}else {
				//显示红色外框,表示不能摆放物体
			}
		}
		/**
		 * 检测是否能够在指定区域摆放物体
		 * @param	row	横向格子数
		 * @param	cols 纵向格子数
		 * @return  是否允许摆放物件
		 */
		public function checkPut(tp:Point,row:int, cols:int):Boolean {
			for (var i:int=0; i < row; i++ ) {
				for (var j:int = 0; j < cols; j++ ) {
					if (int(tp.x+i)>=this.row||int(tp.y+j)>=this.cols) {
						return false;
					}else if(_c[tp.x + i][tp.y + j] == 1){
						return false;
					}
				}
			}
			return true;
		}
		private function drawRectangle(e:EventZheng):void //描边
		{
			var targetPoint:Point = e.obj.point;
			var GridFrame:Class = ApplicationDomain.currentDomain.getDefinition("GridFrame") as Class;
			
			if (gridFrame == null) {
				gridFrame = new GridFrame();
			}
			var cx:int = (targetPoint.x + targetPoint.y) * MapData.basePix;
			var cy:int = (targetPoint.x - targetPoint.y) * MapData.basePix * 0.5;
			gridFrame.x = cx;
			gridFrame.y = cy;
			gridFrame.mouseEnabled = false;
			addChild(gridFrame);
			
			if (checkPut(targetPoint, _siteNum.x, _siteNum.y)) {
				gridFrame.gotoAndStop(1);
			}else {
				gridFrame.gotoAndStop(2);
			}
		}
		private function removeRectangle(e:EventZheng):void 
		{
			if (gridFrame != null) {
				removeChild(gridFrame);
				gridFrame = null;
			}
		}
		public function changeC(sp:Point, row:int, cols:int, num:int):void {
			for (var i:int = 0; i < row; i++ ) {	
				for (var j:int=0; j < cols; j++ ) {
					_c[sp.x + i][sp.y + j] = num;
					var tile:Tile = getChildByName("t" + int(sp.x + i) + "_" + int(sp.y+j)) as Tile;
					tile.goto(num + 1);
				}
			}
		}
		public function set c(value:Array):void 
		{
			_c = value;
		}
		
		public function get c():Array { return _c; }
		
		public function set siteNum(value:Point):void 
		{
			_siteNum = value;
		}
		
		public function get mapController():MapController { return _mapController; }
		
		public function set mapController(value:MapController):void 
		{
			_mapController = value;
		}
		
		public function get mapDataManage():MapDataManage { return _mapDataManage; }
		
		public function set mapDataManage(value:MapDataManage):void 
		{
			_mapDataManage = value;
		}
		
	}
}